
<script>
    myApp.controller('SearchCtrl', function ($scope, ngDialog, $http, $sce) {
        $scope.search = function (search, start, rows) {
            var query = search;
            if (search.indexOf("title:") >= 0 || search.indexOf("content:") >= 0) {
            } else {
                // http://172.16.16.136:8983/solr/files/select?fl=title,content,source,author,updatedDate,id&hl.fl=title,content&hl.simple.post=%3C/em%3E&hl.simple.pre=%3Cem%20style=%27key%27%3E&hl=on&indent=on&q=title:%22Flyme%20Design%20App%22%20or%20content=%22Flyme%20Design%20App%22&rows=2&wt=json
                query = 'title:"' + search + '"^10 OR  author:"' + search + '"^9 OR content:"' + search + '"^8' + ' OR "' + search + '"^7' + ' OR ' + search;
            }
            var url = "/search?fl=title,source,author,updatedDate,id&hl.fl=title,content&hl.simple.post=%3C/span%3E&hl.simple.pre=%3Cspan%20class=%27search-key%27%3E&hl=on&q=" + query + "&rows=" + rows + "&start=" + start + "&wt=json";
            $http({
                method: 'GET',
                url: url,
                headers: getSafeRequestHeader()
            }).success(function (response) {

                console.log(response);
                $scope.response = response;
                if (response.response.docs.length == 0) {
                    $scope.data = null;
                } else {
                    $scope.data = response.response.docs;
                }
                $scope.searchKeyword = search;
            }).error(function (response) {
                console.log(response);
            });
        };

        $scope.search(keyWords, 0, 10);
        $scope.filterContent = function (content, searchValue) {
            if (searchValue == undefined || content == undefined) {
                return content;
            }

            var items = content.split("\n");
            for (var i in items) {
                var index = items[i].toLowerCase().indexOf(searchValue.toLowerCase());
                if (index >= 0) {
                    item = removeHTMLTag(items[i]);
                    index = item.toLowerCase().indexOf(searchValue.toLowerCase());
                    var line = item.substr(0, index) + '<span class="search-key">' + item.substr(index, searchValue.length) + '</span>' + item.substr(index + searchValue.length, item.length);
                    return line
                }
            }
            return items.length > 0 ? removeHTMLTag(items[0]) : removeHTMLTag(content);
        };
        $scope.hightlinghtingContent = function (item) {
            if ($scope.response.highlighting[item.id].content == undefined) {
                return $scope.filterContent(item.content, $scope.searchKeyword)
            }
            return removeHTMLTag($scope.response["highlighting"][item.id].content[0]);
        };
        $scope.hightlinghtingTitle = function (item) {
            if ($scope.response.highlighting[item.id].title == undefined) {
                return $scope.filterContent(item.title, $scope.searchKeyword)
            }
            return removeHTMLTag($scope.response["highlighting"][item.id].title[0]);
        };


        $scope.dateFormat = function (datetime) {
            return new Date(datetime).format("yyyy-MM-dd hh:mm");
        };
        $scope.searchKeyup = function (e) {
            var keycode = window.event ? e.keyCode : e.which;
            if (keycode == 13) {
                $scope.search($scope.searchValue, 0, 10);
            }
        }
    });
    function removeHTMLTag(str) {
        //console.log(str);
        str = str.replaceAll("<span class='select-key'>", "%&&%");
        str = str.replaceAll("</span>", "%##%");
        str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag

        console.log(str);
        str = str.replaceAll("%&&%", "<span class='select-key'>");
        str = str.replaceAll("%##%", "</span>");
        //str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
        //str = str.replace(/\n[\s| | ]*\r/g, '\n'); //去除多余空行
        //str = str.replace(/ /ig, '');//去掉
        console.log(str);
        return str;
    }
    String.prototype.replaceAll = function (s1, s2) {
        return this.replace(new RegExp(s1, "gm"), s2);
    }
</script>
<style>
    html {
        height: 100%;
    }

    body {
        font: 14px/1.5 Helvetica, Tahoma, Arial, "Hiragino Sans GB", "Microsoft YaHei", SimSun, Heiti, sans-serif;
        height: 100%;
    }

</style>
<div ng-controller="SearchCtrl" class="search-content container">

    <div class="searchLayout //data!=null?'show':''//">
        <div class="search-form">
            <span class="search-icon glyphicon glyphicon-search btn-lg"></span>
            <input ng-model="searchValue" class="search" placeholder="请输入搜索内容" ng-keyup="searchKeyup($event)"/>
            <button class="search-button" ng-click="search(searchValue, 0,10)">搜索</button>
        </div>

        <p class="search-empty" ng-if="searchKeyword&&data==null">搜索结果为空</p>
    </div>
    <ul ng-if="data" class="search-result">
        <p style="color:#777;font-size: 16px;">我们为你找到了<nobr>1234</nobr>个相关内容</p>
        <li class="item" ng-repeat="item in data">
            <a class="title" target="_blank" href="//item.id//"
               ng-bind-html="snippet(hightlinghtingTitle(item))">
            </a>

            <p>
               <span class="time" ng-bind="'来源: '+item.source"></span>
                <span class="author" ng-bind="'作者: '+item.author[0]"></span>
                <span class="time" ng-bind="'时间: '+dateFormat(item.updatedDate)"></span>

            </p>

            <div class="item-content" ng-bind-html="snippet(hightlinghtingContent(item))"></div>
        </li>
    </ul>
    <span id="content_body" hidden></span>


</div>
